package com.mantru.iotpot.dishes

import android.app.Application
import android.util.Log
import androidx.databinding.ObservableField
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.mantru.iotpot.Bean.DishesCreateBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONObject

class DishesTimePageViewModel (application: Application) : AndroidViewModel(application) {
    //持续时间，实现双向绑定
    var allTime = ObservableField<String>("持续时间：")
    var spec = ObservableField<String>("1份")
    var vegetableName = ObservableField<String>("")
    //其他页面intent传过来的数据
    var steep_type_List = ObservableField<MutableList<String>>()
    var steep_index_List = ObservableField<MutableList<String>>()
    var fire_value_List = ObservableField<MutableList<String>>()
    var time_value_List = ObservableField<MutableList<String>>()
    var time_long_List = ObservableField<MutableList<String>>()
    var gripper_speed_value_List = ObservableField<MutableList<String>>()
    var gripper_rotation_value_List = ObservableField<MutableList<String>>()
    var gripper_position_value_List = ObservableField<MutableList<String>>()
    var nozzle1_value_List = ObservableField<MutableList<String>>()
    var nozzle2_value_List = ObservableField<MutableList<String>>()
    var nozzle3_value_List = ObservableField<MutableList<String>>()
    var nozzle4_value_List = ObservableField<MutableList<String>>()
    var nozzle5_value_List = ObservableField<MutableList<String>>()
    var nozzle6_value_List = ObservableField<MutableList<String>>()
    var nozzle7_value_List = ObservableField<MutableList<String>>()
    var nozzle8_value_List = ObservableField<MutableList<String>>()
    var nozzle9_value_List = ObservableField<MutableList<String>>()
//    var nozzle10_value_List = ObservableField<MutableList<String>>()
//    var nozzle11_value_List = ObservableField<MutableList<String>>()
    var box_need_avoid_List = ObservableField<MutableList<String>>()
    var box_release_type_List = ObservableField<MutableList<String>>()
    //盒子食材名称列表
    var box_name_List_List = ObservableField<MutableList<MutableList<String>>>()
    //盒子食材名称列表
    var box_value_List_List = ObservableField<MutableList<MutableList<String>>>()
    // 暴露给Activity的数据
    private val _dishesCreateList = MutableLiveData<MutableList<DishesCreateBean>>()
    val dishesCreateList: LiveData<MutableList<DishesCreateBean>> = _dishesCreateList

    fun setValue(specDataJson: String){
        try {
            val jsonObject = JSONObject(specDataJson)
            steep_type_List.set(jsonArrayToList(jsonObject.getJSONArray("steep_type_Lists")))
            steep_index_List.set(jsonArrayToList(jsonObject.getJSONArray("steep_index_Lists")))
            fire_value_List.set(jsonArrayToList(jsonObject.getJSONArray("fire_value_Lists")))
            time_value_List.set(jsonArrayToList(jsonObject.getJSONArray("time_value_Lists")))
            time_long_List.set(jsonArrayToList(jsonObject.getJSONArray("time_long_Lists")))
            gripper_speed_value_List.set(jsonArrayToList(jsonObject.getJSONArray("gripper_speed_value_Lists")))
            gripper_rotation_value_List.set(jsonArrayToList(jsonObject.getJSONArray("gripper_rotation_value_Lists")))
            gripper_position_value_List.set(jsonArrayToList(jsonObject.getJSONArray("gripper_position_value_Lists")))
            nozzle1_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle1_value_Lists")))
            nozzle2_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle2_value_Lists")))
            nozzle3_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle3_value_Lists")))
            nozzle4_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle4_value_Lists")))
            nozzle5_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle5_value_Lists")))
            nozzle6_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle6_value_Lists")))
            nozzle7_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle7_value_Lists")))
            nozzle8_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle8_value_Lists")))
            nozzle9_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle9_value_Lists")))
//            nozzle10_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle10_value_Lists")))
//            nozzle11_value_List.set(jsonArrayToList(jsonObject.getJSONArray("nozzle11_value_Lists")))
            box_need_avoid_List.set(jsonArrayToList(jsonObject.getJSONArray("box_need_avoid_Lists")))
            box_release_type_List.set(jsonArrayToList(jsonObject.getJSONArray("box_release_type_Lists")))
            box_name_List_List.set(jsonArrayToListList(jsonObject.getJSONArray("box_name_List_Lists")))
            box_value_List_List.set(jsonArrayToListList(jsonObject.getJSONArray("box_value_List_Lists")))

            //设置等待时间
            if ( time_long_List.get() != null ){
                allTime.set("等待时间：" + formatTotalTime(time_long_List.get()!!))
            }
        } catch (e: Exception){
            Log.e("---setValue", e.toString())
        }
    }

    // 将 JSONArray 转换为 MutableList<String>
    private fun jsonArrayToList(jsonArray: JSONArray): MutableList<String>{
        val mutableList = mutableListOf<String>()
        for (i in 0 until jsonArray.length()) {
            mutableList.add(jsonArray.getString(i))
        }
        return mutableList
    }

    // 将 JSONArray 转换为 MutableList<MutableList<String>>
    private fun jsonArrayToListList(jsonArray: JSONArray): MutableList<MutableList<String>>{
        val outerList = mutableListOf<MutableList<String>>()
        for (i in 0 until jsonArray.length()) {
            val innerJsonArray = jsonArray.getJSONArray(i)
            val innerList = mutableListOf<String>()
            for (j in 0 until innerJsonArray.length()) {
                innerList.add(innerJsonArray.getString(j))
            }
            outerList.add(innerList)
        }
        return outerList
    }

    //将秒数转为时分秒
    fun formatTotalTime(secondsList: MutableList<String>): String {
        val totalSeconds = secondsList
            .map { it.toInt() }
            .sum()
        return formatTime(totalSeconds)
    }

    fun formatTime(totalSeconds: Int): String {
        val hours = totalSeconds / 3600
        val minutes = (totalSeconds % 3600) / 60
        val seconds = totalSeconds % 60

        val builder = StringBuilder()
        if (hours > 0) {
            builder.append(hours.toString()+"时")
        }
        if (minutes > 0) {
            builder.append(minutes.toString()+"分")
        }
        builder.append(seconds.toString()+"秒")
        return builder.toString()
    }

    // 更新菜谱研发列表数据
    fun updateDishesList() {
        //确保在主线程进行
        viewModelScope.launch(Dispatchers.Main) {
            val newList = mutableListOf<DishesCreateBean>()
            if (steep_index_List.get()?.isNotEmpty() == true){
                for ( i in 0 until steep_index_List.get()!!.size){
                    newList.add(
                        DishesCreateBean(
                            steep_type = steep_type_List.get()?.get(i).toString(),
                            steep_index = steep_index_List.get()?.get(i).toString(),
                            fire_value = fire_value_List.get()?.get(i).toString(),
                            time_value = time_value_List.get()?.get(i).toString(),
                            time_long = time_long_List.get()?.get(i).toString(),
                            gripper_speed_value = gripper_speed_value_List.get()?.get(i).toString(),
                            gripper_rotation_value = gripper_rotation_value_List.get()?.get(i).toString(),
                            gripper_position_value = gripper_position_value_List.get()?.get(i).toString(),
                            nozzle1_value = nozzle1_value_List.get()?.get(i).toString(),
                            nozzle2_value = nozzle2_value_List.get()?.get(i).toString(),
                            nozzle3_value = nozzle3_value_List.get()?.get(i).toString(),
                            nozzle4_value = nozzle4_value_List.get()?.get(i).toString(),
                            nozzle5_value = nozzle5_value_List.get()?.get(i).toString(),
                            nozzle6_value = nozzle6_value_List.get()?.get(i).toString(),
                            nozzle7_value = nozzle7_value_List.get()?.get(i).toString(),
                            nozzle8_value = nozzle8_value_List.get()?.get(i).toString(),
                            nozzle9_value = nozzle9_value_List.get()?.get(i).toString(),
//                            nozzle10_value = nozzle10_value_List.get()?.get(i).toString(),
//                            nozzle11_value = nozzle11_value_List.get()?.get(i).toString(),
                            nozzle10_value = "0",
                            nozzle11_value = "0",
                            box_need_avoid = box_need_avoid_List.get()?.get(i).toString(),
                            box_release_type = box_release_type_List.get()?.get(i).toString(),
                            foodNameList = box_name_List_List.get()?.get(i)?: mutableListOf(),
                            foodValueList = box_value_List_List.get()?.get(i)?: mutableListOf(),
                        )
                    )
                }
            }
            _dishesCreateList.postValue(newList)
        }
    }
}